package top.caoayu.common.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.interceptor.CustomizableTraceInterceptor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@EnableAspectJAutoProxy(proxyTargetClass = true)
@Aspect
@Configuration
public class TraceMethodAop {

    @Pointcut("execution(* top.caoayu.base.service.*.*(..))")
    public void pointCut(){}

    @Bean
    public Advisor traceAdvisor(){
        /*
        * springboot 自带的日志 aop 日志级别必须为 trace 才会显示。
        * */
        CustomizableTraceInterceptor customizableTraceInterceptor = new CustomizableTraceInterceptor();
        customizableTraceInterceptor.setEnterMessage("执行方法 ：$[targetClassName]:$[methodName]($[arguments])");
        customizableTraceInterceptor.setExitMessage("退出方法：$[methodName]() 返回值：$[returnValue]");
        customizableTraceInterceptor.setUseDynamicLogger(true);
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression("top.caoayu.common.aop.TraceMethodAop.pointCut()");
        return new DefaultPointcutAdvisor(pointcut, customizableTraceInterceptor);
    }
}
